/////////////////////////////////////////////////////////////////
// Script:  rezzes cicp_prims advised by link messages of
//          slcicp_init script
//
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// constants
/////////////////////////////////////////////////////////////////

// command prefixes to put commands in a specific context
integer CMD_ERROR           = 1; // an error occured
integer CMD_RESPONSE        = 2; // message is a response
integer CMD_REQUEST         = 4; // message is a request
// integer CMD_             = 8; // reserved for probable later use

// commands
integer CMD_REZ             = 16; // rezzing cicp_prims
integer CMD_CANCEL          = 32; // cancel current action
integer CMD_START_PARAM     = 64; // request/receive start param (see rezStartParam)
integer CMD_DETECT          = 128; // return script's name
integer CMD_COMPLETE        = 256; // operation has completed (response only)
integer CMD_PROGRESS        = 512; // message contains progress information

integer REZ_PROGRESS_INTERVAL = 50; // interval of prims to report progress of rezzing

/////////////////////////////////////////////////////////////////
// global vars
/////////////////////////////////////////////////////////////////

integer linkNumber          = 0; // slcicp-prim's linknumber
string  scriptName          = ""; // name of script in object's inventory

string  sheepInventoryName  = ""; // name of sheep prims in object's inventory
integer sheepRezNum         = 0; // amount of sheeps prims to rez
integer rezzedSheeps        = 0; // amount of sheeps already rezzed
integer rezStartParam       = 0; // start parameter for first prim, will then be incremented

/////////////////////////////////////////////////////////////////
// functions
/////////////////////////////////////////////////////////////////

// sends message to main script
sendMessage(integer cmd, string data)
{
    llMessageLinked(LINK_THIS, cmd, data, (key)scriptName);
}

/////////////////////////////////////////////////////////////////
// states: default, idle, rezSheeps
/////////////////////////////////////////////////////////////////

 default
 {
     state_entry()
     {
         linkNumber = llGetLinkNumber();
         scriptName = llGetScriptName();
         state idle;
     }
 }

state idle
{
    link_message(integer lN, integer cmd, string data, key id)
    {
        // check if message comes from same prim
        if (lN == linkNumber) {
            // check if message is for this script
            if ((string)id == scriptName) {
                list parameters = llCSV2List(data);

                // process commands
                if (cmd & CMD_REQUEST) {
                    if (cmd & CMD_REZ) {
                        sheepInventoryName = llList2String(parameters, 0);
                        rezStartParam = llList2Integer(parameters, 1);
                        sheepRezNum = llList2Integer(parameters, 2);

                        // rez sheeps
                        state rezSheeps;
                    }
                }
            }
            else if (cmd & CMD_DETECT && cmd & CMD_REQUEST) {
                // tell main script this script's name
                sendMessage(CMD_RESPONSE | CMD_DETECT, scriptName);
            }
        }
    }
}

state rezSheeps
{
    state_entry()
    {
        // start rezzing
        rezzedSheeps = 0;
        vector rezPos = llGetPos();
        do {
            llRezObject(sheepInventoryName, rezPos, ZERO_VECTOR, ZERO_ROTATION, rezStartParam);
            ++rezStartParam;
            ++rezzedSheeps;
            // report progress to slcicp init script every REZ_PROGRESS_INTERVAL prims
            if (rezzedSheeps % REZ_PROGRESS_INTERVAL == 0) {
                sendMessage(CMD_RESPONSE | CMD_REZ | CMD_PROGRESS, (string)REZ_PROGRESS_INTERVAL);
            }
        }
        while (rezzedSheeps < sheepRezNum);

        // finished, send amount of rezzed sheeps to main script
        sendMessage(CMD_RESPONSE | CMD_REZ | CMD_COMPLETE, (string)rezzedSheeps);
        state idle;
    }
}
